﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FSViet.Survey
{
	public abstract class CommonAdminPageBase : Page
	{
		protected Guid SiteId
		{
			get
			{
				Guid result = Guid.Empty;
				if (Request.QueryString["SiteId"] != null)
				{
					result = GlobalApplication.GetGuid(Request.QueryString["SiteId"]);
					SiteId = result;
				}
				else
					result = (Guid)Session["SiteId"];

				return result;
			}
			set { Session["SiteId"] = value; }
		}

		protected Guid CampaignId
		{
			get
			{
				Guid result = Guid.Empty;
				if (Request.QueryString["CampaignId"] != null)
				{
					result = GlobalApplication.GetGuid(Request.QueryString["CampaignId"]);
					CampaignId = result;
				}
				else
					result = (Guid)Session["CampaignId"];

				return result;
			}
			set { Session["CampaignId"] = value; }
		}

		protected Guid QuestionId
		{
			get
			{
				Guid result = Guid.Empty;
				if (Request.QueryString["QuestionId"] != null)
				{
					result = GlobalApplication.GetGuid(Request.QueryString["QuestionId"]);
					QuestionId = result;
				}
				else
					result = (Guid)Session["QuestionId"];

				return result;
			}
			set { Session["QuestionId"] = value; }
		}

		#region check id and auto redirect
		
		protected void CheckSite()
		{
			try
			{
				Guid temp = SiteId;
			}
			catch
			{
				Response.Redirect("../Site/List.aspx",true);
			}
		}

		protected void CheckCampaign()
		{
			CheckSite();

			OnCheckCampaign();
		}

		protected void OnCheckCampaign()
		{
			try
			{
				Guid temp = CampaignId;
			}
			catch
			{
				Response.Redirect("../Campaign/List.aspx");
			}
		}

		protected void CheckQuestion()
		{
			CheckCampaign();

			OnCheckQuestion();
		}

		protected void OnCheckQuestion()
		{
			try
			{
				Guid temp = QuestionId;
			}
			catch
			{
				Response.Redirect("../Question/List.aspx", true);
			}
		}

		#endregion
	}

	public abstract class ListPageBase:CommonAdminPageBase
	{
		protected int PageIndex
		{
			get
			{
				int result = 1;
				if (Request.QueryString["p"] == null)
				{
					result = Session["p"] != null ? (int)Session["p"] : 1;
				}
				else
					try
					{
						result = Convert.ToInt32(Request.QueryString["p"]);
						PageIndex = result;
					}
					catch (Exception)
					{

					}
				return result;
			}
			set { Session["p"] = value; }
		}

		protected void ShowPage(Panel pageListContainer, int totalRecord, string param)
		{
			param = param.Trim();
			if (totalRecord > GlobalApplication.ItemPerPage)
			{ }
			else
			{
				pageListContainer.Visible = false;
				return;
			}

			int totalPage = totalRecord / GlobalApplication.ItemPerPage + 1;
			var ul = new HtmlGenericControl("ol");
			ul.Attributes.Add("class", "pagination");
			for (int i = 1; i <= totalPage; i++)
			{
				var li = new HtmlGenericControl("li");
				if (i == PageIndex)
				{
					li.Attributes.Add("class", "selected");
					li.Controls.Add(new HyperLink() { Text = i.ToString(), NavigateUrl = "" });
				}
				else
					li.Controls.Add(new HyperLink() { Text = i.ToString(), NavigateUrl = "?p=" + i.ToString() + ((param.Length > 0) ? "&" + param : "") });
				ul.Controls.Add(li);
			}
			pageListContainer.Controls.Add(ul);
		}
	}

	public abstract class DetailsPageBase : CommonAdminPageBase
	{
		protected Guid ItemId
		{
			get
			{
				Guid result = Guid.Empty;
				if (Request.QueryString["Id"] != null)
				{
					result = GlobalApplication.GetGuid(Request.QueryString["Id"]);
				}
				return result;
			}
		}
	}
}